package com.hxkj.controller.wechat;

import com.hxkj.domain.po.Applet;
import com.hxkj.domain.po.AppletArticle;
import com.hxkj.domain.po.AppletConfigure;
import com.hxkj.entity.enums.FilePathEnum;
import com.hxkj.service.FileService;
import com.hxkj.service.wechat.AppletArticleService;
import com.hxkj.service.wechat.AppletService;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.result.ResultPageStruct;
import com.platform.common.domain.to.PageTo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.concurrent.Callable;

/**
 * 小程序配置控制层
 * <p>
 * Created by yaosy on 2018年6月12日14:27:10
 */
@RestController
@RequestMapping("/applet")
public class AppletController {

    private static final Logger LOGGER = LoggerFactory.getLogger(AppletController.class);

    @Autowired
    private AppletService appletService;


    @Autowired
    private FileService fileService;


    /**
     * 小程序配置列表
     * @param
     * @return
     * @throws Exception
     */
    @RequiresPermissions("applet:view")
    @RequestMapping(value = "/getlist", method = RequestMethod.POST)
    public Callable<ResultPageStruct<AppletConfigure>> getlist(@ModelAttribute AppletConfigure appletConfigure, @ModelAttribute PageTo pageParam) throws Exception{
        return () -> appletService.getlist(appletConfigure,pageParam);
    }

    /**
     * 新增小程序
     *
     * @param appletConfigure
     * @throws Exception
     */
    @RequiresPermissions("applet:add")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public void save(@ModelAttribute AppletConfigure appletConfigure) throws Exception {
         appletService.save(appletConfigure);
    }


    /**
     * 单条小程序数据
     *
     * @param appletConfigure
     * @throws Exception
     */
    @RequestMapping(value = "/get", method = RequestMethod.POST)
    public Callable<AppletConfigure> get(@ModelAttribute AppletConfigure appletConfigure) throws Exception {
        return ()-> appletService.get(appletConfigure);
    }

    /**
     * 单条小程序修改保存
     * @param
     * @return
     * @throws Exception
     */
    @RequiresPermissions("applet:modify")
    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    public void modify(@ModelAttribute AppletConfigure appletConfigure) throws Exception {
        appletService.modify(appletConfigure);
    }


    /**
     * 小程序文件上传
     * @param
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public Callable<List<String>> upload(@RequestParam(value = "file", required = false) MultipartFile[] file) throws Exception {
        return () -> fileService.appletUpload(file, FilePathEnum.APPLET_FILE_PATH.getDescription());
    }

    /**
     * 小程序模式信息
     * @param
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getModeList", method = RequestMethod.POST)
    public Callable<AppletConfigure> getModeList(@ModelAttribute AppletConfigure appletConfigure) throws Exception {
        return () -> appletService.getModeList(appletConfigure);
    }
}
